package com.scm.controller;

import com.scm.pojo.Customer;
import com.scm.service.CustomerService;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CustomerController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/14 9:31
 * @Version 1.0
 **/
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/customerList")
    public QueryResult customerList(Integer currentPage, Integer pageSize){
        Map map = Page.get(currentPage,pageSize);
        List list =customerService.getPageCustomer(map);
        int total = customerService.count();
        return QueryResult.ok(total,list);
    }

    @RequestMapping("/addCustomer")
    public Result addCustomer(){
         List list =customerService.getAllCustomerCode();
         String customerCode = Util.getID(list,"c");
         return Result.ok(customerCode);
    }
    @RequestMapping("/addCustomer2")
    public Result addCustomer2(Customer customer){
        customer.setCreateDate(new SimpleDateFormat("yyyy-MM--dd hh:mm:ss").format (new Date()));
        customerService.addCustomer(customer);
        return Result.ok(null);
    }

    @RequestMapping("/deleteCustomer")
    public Result deleteCustomer(String customerCode){
        //待判断-------------------------
        customerService.deleteCustomer(customerCode);
        return Result.ok(null);
    }

    @RequestMapping("/updateCustomer")
    public  Result updateCustomer(String customerCode){
        Customer customer = customerService.findCustomer(customerCode);
        return Result.ok(customer);
    }

    @RequestMapping("/updateCustomer2")
    public Result updateCustomer2(Customer customer){
        System.out.println(customer);
        customerService.updateCustomer(customer);
        return Result.ok(null);
    }
    @RequestMapping("selectCustomer")
    public QueryResult selectCustomer(Integer currentPage, Integer pageSize,String customerCode,String name,String contactor){
        System.out.println(customerCode);
        Map<String, Object> map = Page.get(currentPage, pageSize);
        map.put("customerCode",customerCode);
        map.put("name",name);
        map.put("contactor",contactor);
        System.out.println(map);
        int total=customerService.selectCountCustomer(map);
        List<Customer> list = customerService.selectCustomer(map);
        return QueryResult.ok(total,list);
    }
}
